package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.mapper.OutStockMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.business.service.OutStockService;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.OutStock;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class OutStockServiceImpl implements OutStockService {
    @Autowired
    private OutStockMapper outStockMapper;

    //加载表格数据
    @Override
    public PageVO<OutStockVO> findOutStockList(Map<String, Object> map) {
        PageHelper.startPage(Integer.valueOf((String)map.get("pageNum")),
                Integer.valueOf((String) map.get("pageSize")));

        Example example = new Example(OutStock.class);
        Example.Criteria criteria = example.createCriteria();

        String outNmu = (String) map.get("outNum");
        if (outNmu != null && !"".equals(outNmu)){
            criteria.andEqualTo("outNum",outNmu);
        }

        String status = (String) map.get("status");
        if (status != null && !"".equals(status)){
            criteria.andEqualTo("status",status);
        }

        String type = (String) map.get("type");
        if (type != null && !"".equals(type)){
            criteria.andEqualTo("type",type);
        }

        List<OutStock> list = outStockMapper.selectByExample(example);
        PageInfo<OutStock> pageInfo = new PageInfo<>(list);
        List<OutStockVO> listVO = list.stream().map(outStock -> {
            OutStockVO outStockVO = new OutStockVO();
            BeanUtils.copyProperties(outStock, outStockVO);
            Long consumerId = outStock.getConsumerId();
            Consumer consumer = new Consumer();
            consumer.setId(consumerId);
            Consumer consumer1 = consumerMapper.selectByPrimaryKey(consumer);
            outStockVO.setAddress(consumer1.getAddress());
            outStockVO.setPhone(consumer1.getPhone());
            outStockVO.setName(consumer1.getName());
            return outStockVO;
        }).collect(Collectors.toList());

        return new PageVO<OutStockVO>(pageInfo.getTotal(),listVO);
    }
    @Autowired
    private ConsumerMapper consumerMapper;

    //放入回收站
    @Override
    public void remove(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        OutStock outStock1 = outStockMapper.selectByPrimaryKey(outStock);
        outStock1.setStatus(1);
        outStockMapper.updateByPrimaryKey(outStock1);
    }

    @Override
    public void back(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        OutStock outStock1 = outStockMapper.selectByPrimaryKey(outStock);
        outStock1.setStatus(0);
        outStockMapper.updateByPrimaryKey(outStock1);
    }

    @Override
    public void publish(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        OutStock outStock1 = outStockMapper.selectByPrimaryKey(outStock);
        outStock1.setStatus(0);
        outStockMapper.updateByPrimaryKey(outStock1);
    }

    @Override
    public void delete(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStockMapper.deleteByPrimaryKey(outStock);
    }

    //添加发放记录
    @Override
    public void addOutStock(OutStockVO outStockVO) {
        OutStock outStock = new OutStock();
        BeanUtils.copyProperties(outStock,outStockVO);

        //对库存仓库进行操作
        List<Object> products = outStockVO.getProducts();
        Map map = (Map) products.get(0);
        Integer productId = (Integer) map.get("productId");
        Integer productNumber = (Integer) map.get("productNumber");
        Product p = new Product();
        p.setId(Long.valueOf(String.valueOf(productId)));
        Product product = productMapper.selectByPrimaryKey(p);
        String pNum = product.getPNum();
        Example example = new Example(ProductStock.class);
        example.createCriteria().andEqualTo("pNum",pNum);
        ProductStock stock1 = productStockMapper.selectOneByExample(example);
        long number = stock1.getStock() - productNumber;
        if (number < 0){
             throw new RuntimeException("库存不够");
        }
        stock1.setStock(number);
        productStockMapper.updateByPrimaryKey(stock1);

        String uuid = UUID.randomUUID().toString();
        uuid = uuid.substring(0,32);
        outStock.setOutNum(uuid);
        //假的
        outStock.setType(3);
        outStock.setOperator("admin");
        outStock.setCreateTime(new Date());
        outStock.setProductNumber(productNumber);
        outStock.setConsumerId(27L);
        outStock.setStatus(2);
        outStock.setPriority(2);
        outStock.setRemark("常规发放");
        outStockMapper.insert(outStock);
    }
    @Autowired
    private ProductStockMapper productStockMapper;
    @Autowired
    private ProductMapper productMapper;
}
